package com.fengzai.demo.config;

import com.fengzai.common.res.R;
import com.fengzai.netty.common.callback.HandlerProcessor;
import com.fengzai.netty.common.domain.UdpMsgInfo;
import com.fengzai.netty.udp.UdpClient;
import com.fengzai.netty.udp.UdpServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @PACKAGE_NAME: com.fengzai.demo.config
 * @author: rhf
 * @ProjectName: netty
 * @DATE: 2021/10/11
 **/
@Configuration
public class UdpConfiguration {
    private Integer udpServerPort = 10001;
    private Integer udpClientPort = 10002;
    @Bean(destroyMethod = "stop")
    public UdpServer udpServer() {
        HandlerProcessor<UdpMsgInfo, Object> handlerProcessor = new HandlerProcessor<UdpMsgInfo, Object>() {
            @Override
            public Object process(UdpMsgInfo msgInfo) {
                return null;
            }
        };
        UdpServer udpServer = new UdpServer(handlerProcessor);
        new Thread() {
            @Override
            public void run() {
                udpServer.run(udpServerPort);
            }
        }.start();
        return udpServer;
    }

    @Bean(destroyMethod = "stop")
    public UdpClient udpClient() {
        HandlerProcessor<UdpMsgInfo, Object> handlerProcessor = new HandlerProcessor<UdpMsgInfo, Object>() {
            @Override
            public Object process(UdpMsgInfo msgInfo) {
                return null;
            }
        };
        UdpClient udpClient = new UdpClient(handlerProcessor, 1000 * 10);
        new Thread() {
            @Override
            public void run() {
                udpClient.run(udpClientPort);
            }
        }.start();
        return udpClient;
    }

}
